# Contributor: Holger Jaekel <holger.jaekel@gmx.de>
# Maintainer: Holger Jaekel <holger.jaekel@gmx.de>
pkgname=tomcat9
pkgver=9.0.84
pkgrel=0
pkgdesc="Open source implementation of the Java Servlet 4.0 and JavaServer Pages 2.3 technologies"
url="https://tomcat.apache.org/"
arch="noarch !x86 !armv7 !armhf !ppc64le !riscv64"  # eclipse-ecj availability
license="Apache-2.0"
depends="eclipse-ecj java-common"
pkgusers="tomcat9"
pkggroups="tomcat9"
checkdepends="java-jre-headless"
install="$pkgname.pre-install"
subpackages="
	$pkgname-openrc
	$pkgname-admin
	$pkgname-doc
	$pkgname-examples
	"

source="
	https://archive.apache.org/dist/tomcat/tomcat-9/v$pkgver/bin/apache-tomcat-$pkgver.tar.gz
	10-daemon.patch
	"
builddir=$srcdir/apache-${pkgname//9/}-$pkgver

# secfixes:
#   9.0.80-r0:
#     - CVE-2023-41080
#   9.0.73-r0:
#     - CVE-2023-28708
#   9.0.71-r0:
#     - CVE-2023-24998
#   9.0.70-r0:
#     - CVE-2022-45143
#   9.0.68-r0:
#     - CVE-2022-42252
#   9.0.64-r0:
#     - CVE-2022-34305
#   9.0.63-r0:
#     - CVE-2022-29885
#   9.0.58-r0:
#     - CVE-2022-23181

check() {
	$builddir/bin/configtest.sh
}

package() {
	# Tomcat general files
	install -dm 755 "$pkgdir"/usr/share/$pkgname
	install -dm 755 "$pkgdir"/usr/share/$pkgname/bin
	install -dm 755 "$pkgdir"/usr/share/java/$pkgname
	cp -dr bin "$pkgdir"/usr/share/$pkgname
	# commons-daemon and tomcat-natives are packaged on their own
	chmod 644 "$pkgdir"/usr/share/$pkgname/bin/*.jar
	chmod 755 "$pkgdir"/usr/share/$pkgname/bin/*.sh
	rm "$pkgdir"/usr/share/$pkgname/bin/*.bat
	rm "$pkgdir"/usr/share/$pkgname/bin/commons-daemon*
	rm "$pkgdir"/usr/share/$pkgname/bin/daemon.sh
	rm "$pkgdir"/usr/share/$pkgname/bin/tomcat-native.tar.gz

	install -m 644 lib/* "$pkgdir"/usr/share/java/$pkgname
	# eclipse-ecj is packaged on its own
	rm "$pkgdir"/usr/share/java/$pkgname/ecj-*.jar
	ln -s ../eclipse-ecj.jar "$pkgdir"/usr/share/java/$pkgname/ecj.jar

	ln -s /usr/share/java/$pkgname "$pkgdir"/usr/share/$pkgname/lib

	install -dm 775 -o $pkgusers "$pkgdir"/var/log/$pkgname
	ln -s /var/log/$pkgname "$pkgdir"/usr/share/$pkgname/logs

	install -dm 775 "$pkgdir"/etc/$pkgname
	install -m 640 -g $pkggroups conf/* "$pkgdir"/etc/$pkgname
	install -dm 775 -g $pkggroups "$pkgdir"/etc/$pkgname/Catalina
	ln -s /etc/$pkgname "$pkgdir"/usr/share/$pkgname/conf

	install -dm 775 "$pkgdir"/var/lib/$pkgname
	cp -r webapps "$pkgdir"/var/lib/$pkgname
	chown -R $pkgusers:$pkggroups "$pkgdir"/var/lib/$pkgname
	chmod 775 "$pkgdir"/var/lib/$pkgname/webapps
	ln -s /var/lib/$pkgname/webapps "$pkgdir"/usr/share/$pkgname/webapps

	install -d -o $pkgusers -g $pkggroups -m 755 "$pkgdir"/var/cache/$pkgname
	ln -s /tmp "$pkgdir"/usr/share/$pkgname/temp
	ln -s /var/cache/$pkgname "$pkgdir"/usr/share/$pkgname/work
}

admin() {
	pkgdesc="$pkgdesc (admin web applications)"
	_copy_webapp manager
	_copy_webapp host-manager
}

doc() {
	pkgdesc="$pkgdesc (documentation)"
	_copy_webapp docs
}

examples() {
	pkgdesc="$pkgdesc (example web applications)"
	_copy_webapp examples
}

_copy_webapp() {
	depends="$pkgname"

	cd $builddir

	mkdir -p $subpkgdir/var/lib/tomcat9/webapps
	mv $pkgdir/var/lib/tomcat9/webapps/$1 $subpkgdir/var/lib/tomcat9/webapps
	chown -R $pkgusers:$pkggroups "$pkgdir"/var/lib/tomcat9
	chmod 775 "$pkgdir"/var/lib/tomcat9/webapps
}

openrc() {
	depends="commons-daemon jsvc $pkgname"
	pkgdesc="$pkgdesc (OpenRC init scripts)"

	cd $builddir
	install -Dm 755 bin/daemon.sh "$subpkgdir"/etc/init.d/tomcat9
	install -dm 755 "$subpkgdir"/usr/share/tomcat9/bin
	ln -s /usr/share/java/commons-daemon.jar "$subpkgdir"/usr/share/tomcat9/bin/commons-daemon.jar
}

sha512sums="
85a42ab5e7e4cb1923888e96a78a0f277a870d06e76147a95457878c124001c9a317eade4ad69c249a460ffe2cbefe894022b84389cdf33038bc456e3699c8e3  apache-tomcat-9.0.84.tar.gz
7d16b14977c22e1ae998d15a0a83a574624b62c722a0adbfd05a4105581eee347d38e366a269ebca4c8fa894c3718d6fbdcea0e0974d20407075ec2af6ea07a4  10-daemon.patch
"
